import Vue from 'vue'
import Vuex from 'vuex'
import MT from './js/mutation-types'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    appType: undefined,
    isLoading: false,
    token: undefined,
    mainMenus: undefined,
    userCode: undefined,
    userName: undefined,
    user: {},
    mainSelected: '',
    mainSelectedName: '',
    statisticsSelected: '',
    statisticsSelectedName: '',
    mainTabbarType: 'main'
  },
  mutations: {
    [MT.SET_APP_TYPE] (state, data) {
      state.appType = data
    },
    [MT.SET_MAIN_MENUS] (state, data) {
      state.mainMenus = data
    },
    [MT.SET_USER_NAME] (state, data) {
      state.userName = data
    },
    [MT.SET_USER] (state, data) {
      state.user = data
    },
    [MT.SET_USER_CODE] (state, data) {
      state.userCode = data
    },
    [MT.UPDATE_LOADING_STATUS] (state, payload) {
      state.isLoading = payload.isLoading
    },
    [MT.SET_TOKEN] (state, token) {
      state.token = token
    },
    [MT.CLEAR_TOKEN] (state) {
      state.token = undefined
    },
    [MT.SET_MAIN_SELECTED] (state, payload) {
      state.mainSelected = payload.id
      state.mainSelectedName = payload.name
    },
    [MT.SET_STATISTICS_SELECTED] (state, payload) {
      state.statisticsSelected = payload.id
      state.statisticsSelectedName = payload.name
    },
    [MT.SET_MAIN_TABBAR_TYPE] (state, value) {
      state.mainTabbarType = value
    },
    [MT.CLEAR_STATE] (state) {
      for (let s in state) {
        s = undefined
      }
    }
  },
  actions: {}
})
